package com.ruoyi.Api;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.countrybus.domain.SysDriver;
import com.ruoyi.project.countrybus.service.ISysDriverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("司机端API")
@RestController
@RequestMapping("/api/driver")
public class DriverApi extends BaseController
{

    @Autowired
    private ISysDriverService sysDriverService;


    @ApiOperation("司机端登陆验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "driverName", value = "司机账号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/login/{driverName}/{password}")
    public AjaxResult driverlogin(@PathVariable("driverName") String driverName, @PathVariable("password") String password)
    {

        SysDriver sysDriver = sysDriverService.selectSysDriverByDriverName(driverName);
        String pass = sysDriver.getPassword();

        if (StringUtils.equals(pass,password))
        {
            return AjaxResult.success(sysDriver);
//            return AjaxResult.success("登陆成功");
        }
        else
        {
            return error("用户不存在");
        }
    }


    @ApiOperation("司机端找回密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phonenumber", value = "司机手机号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/findpassword/{phonenumber}/{password}")
    public AjaxResult driverchangepassword(@PathVariable("phonenumber") String phonenumber,@PathVariable("password") String password)
    {

        SysDriver sysDriver = sysDriverService.selectSysDriverByDriverPhonenumber(phonenumber);
        sysDriver.setPassword(password);

        return toAjax(sysDriverService.updateSysDriver(sysDriver));
    }


    @ApiOperation("删除司机信息")
    @DeleteMapping("/{driverIds}")
    public AjaxResult remove(@PathVariable Long[] driverIds)
    {
        return toAjax(sysDriverService.deleteSysDriverByDriverIds(driverIds));
    }


    @ApiOperation("修改司机信息")
    @PutMapping
    public AjaxResult edit(@RequestBody SysDriver sysDriver)
    {
        return toAjax(sysDriverService.updateSysDriver(sysDriver));
    }


    @ApiOperation("新增司机信息")
    @PostMapping
    public AjaxResult add(@RequestBody SysDriver sysDriver)
    {
        return toAjax(sysDriverService.insertSysDriver(sysDriver));
    }


    @ApiOperation("通过账号获取司机详情信息")
    @ApiImplicitParam(name = "driverName", value = "司机ID", required = true, dataType = "string", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/name/{driverName}")
    public AjaxResult getInfoByName(@PathVariable("driverName") String driverName)
    {
        return AjaxResult.success(sysDriverService.selectSysDriverByDriverName(driverName));
    }



    @ApiOperation("获取司机详情信息")
    @ApiImplicitParam(name = "driverId", value = "司机ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/{driverId}")
    public AjaxResult getInfo(@PathVariable("driverId") Long driverId)
    {
        return AjaxResult.success(sysDriverService.selectSysDriverByDriverId(driverId));
    }


    @ApiOperation("获取司机列表")
    @GetMapping("/list")
    public TableDataInfo list(SysDriver sysDriver)
    {
        startPage();
        List<SysDriver> list = sysDriverService.selectSysDriverList(sysDriver);
        return getDataTable(list);
    }




}
